#include "CQSpinBoxDelegate.h"

#include <QSpinBox>

CQSpinBoxDelegate::CQSpinBoxDelegate(QObject *parent) :QStyledItemDelegate(parent)
{

}

QWidget *CQSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSpinBox *spinBox = new QSpinBox(parent);
    spinBox->setRange(0, 750);
    return spinBox;
}

void CQSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    //获取模型中的数据
    int val = index.model()->data(index, Qt::DisplayRole).toInt();
    //放到编辑器
    spinBox->setValue(val);
}

void CQSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    int val = spinBox->value();
    model->setData(index, val);
}

void CQSpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}
